package com.zy.edu.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.edu.bean.*;
import com.zy.edu.service.UserService;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-13
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    /**
     * @Author: HuangHaiShui
     * @Description: 数据的新增与修改
     * @Date: 9:17 2021/7/14
     * @param user:
     * @return: Map
     **/
    @RequestMapping("/edit")
    public Map edit(@Valid User user,InputParam param ){
        return userService.saveOrUpdate(user,param);
    }


    /**
     * @Author: HuangHaiShui
     * @Description:数据数据的查询，可分页可查全部
     * @Date: 9:17 2021/7/14
     * @param user:    name 模糊查询
     * @param inputParam:  分页条件
     * @return: Map
     **/
    @RequestMapping("/list")
    public Map list( User user, InputParam inputParam){

        return userService.list(user, inputParam);
    }

    /**
     * @Author: HuangHaiShui
     * @Description: 删除数据
     * @Date: 9:18 2021/7/14
     * @param user:  传入id 字段
     * @return: Map
     **/
    @RequestMapping("/dele")
    public Map dele( User user){
        return userService.dele(user);
    }


    /**
     * @Author: HuangHaiShui 
     * @Description:登录成功后的接口，返回用户的各种信息
     *        已废弃，不用了
     * @Date: 15:51 2021/7/14
     * @param inputParam:
     * @return: Map
     **/
    @RequestMapping("/info")
    public Map info( InputParam inputParam){
        return userService.info(inputParam);
    }

    /**
     * @Author: HuangHaiShui 
     * @Description:获取当前用户的信息
     * @Date: 14:50 2021/7/15
     * @param inputParam: 
     * @return: Map
     **/
    @RequestMapping("/getById")
    public Map getById(@Valid InputParam inputParam){
        return userService.getById(inputParam);
    }

    /**
     * @Author: HuangHaiShui
     * @Description:重置密码
     * @Date: 16:54 2021/7/15
     * @param inputParam:
     *                  id ：要重置的用户id
     * @return: Map
     **/
    @RequestMapping("resetPassword")
    public Map resetPass( InputParam inputParam  ){
        return userService.resetPass(inputParam);
    }
    
    /**
     * @Author: HuangHaiShui 
     * @Description:修改密码
     * @Date: 16:54 2021/7/15
     * @param inputParam:
     *                  userId 当前账号
     * @return: Map
     **/
    @RequestMapping("modifyPass")
    public Map modifyPass( InputParam inputParam  ){

        return userService.modifyPass(inputParam);
    }


    /**
     * @Author: HuangHaiShui
     * @Description:头像的上传
     * @Date: 10:32 2021/7/15
     * @param file:
     * @param inputParam: userId
     * @return: Map
     **/
    @RequestMapping("photo")
    public Map photo(MultipartFile file, @Valid InputParam inputParam  ) throws IOException {

        return userService.photo(file,inputParam);
    }

    /**
     * @Author: HuangHaiShui
     * @Description:根据班级id导出学生
     * @Date: 9:05 2021/7/16
     * @param inputParam: 传入 userId  cid
     * @return: Map
     **/
    @RequestMapping("exportByCid")
    public void exportByCid(HttpServletResponse response, InputParam inputParam  ) throws IOException {

         userService.exportByCid(response,inputParam);
    }
    @RequestMapping("importByCid")
    public Map importByCid( MultipartFile file, InputParam inputParam  ) throws IOException {

         return userService.importByCid(file,inputParam);
    }


    /**
     * @Author: HuangHaiShui 
     * @Description:学生导入模板excel的下载
     * @Date: 14:08 2021/8/17
     * @param response: 
     * @return: void
     **/
    @RequestMapping("exportTemplate")
    public void exportTemplate(HttpServletResponse response) throws IOException {
        //导出excel
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("学生导入模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream(), UserTem.class)
                .autoCloseStream(Boolean.FALSE)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("模板").doWrite(null);

    }

    /**
     * @Author: HuangHaiShui
     * @Description:根据当前角色，获取能查询的老师信息
     * @Date: 13:59 2021/7/17
     * @param inputParam
     *                   userId:当前角色id
     *                   schoolId: 学校id
     * @return: Map
     **/
    @RequestMapping("getTeachers")
    public Map getTeachers( InputParam inputParam ){

        return userService.getTeachers(inputParam);
    }

    /**
     * @Author: HuangHaiShui
     * @Description:根据班级id查询学生
     * @Date: 16:25 2021/7/20
     * @param inputParam:
     * @return: Map
     **/
    @RequestMapping("getStuByClassId")
    public Map getStuByClassId( InputParam inputParam ){

        return userService.getStuByClassId(inputParam);
    }

}

